package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.UuidUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.core.OutUrlConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author ruoyi
 * @data 2024/7/6
 * @apiNote
 */
@Slf4j
@Service
public class HUANHAOBAAttr implements AdapterAttr {

//    @Resource
//    private RedisCache redisCache;
//
//    private static String SERVICE_NAME = "HUAN_HAO_BA";

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        List<GamesRes> resultList = new ArrayList<>();
        String allGame = getAllGame();
        JSONObject allGameJson = JSONObject.parseObject(allGame);
        List<JSONObject> array = JSONObject.parseArray(allGameJson.getString("data"), JSONObject.class);
        for (JSONObject json : array) {
            GamesRes res = new GamesRes();
            res.setId(json.getString("id"));
            res.setGameName(json.getString("gameName"));
            resultList.add(res);
        }
        return resultList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        List<InterServerRes> resultList = new ArrayList<>();
        String qufu = qufu(gameId, salePartner);
        List<JSONObject> array = JSONObject.parseArray(JSONObject.parseObject(qufu).getString("data"), JSONObject.class);
        for (JSONObject json : array) {
            InterServerRes res = new InterServerRes();
            res.setId(json.getString("id"));
            res.setName(json.getString("name"));
            {
                List<InterServerRes> childrenList = new ArrayList<>();
                String childrenShuXing = qufu(json.getString("id"), salePartner);
                JSONObject jsonObject = JSONObject.parseObject(childrenShuXing);
                if (CollectionUtils.isNotEmpty(jsonObject.getJSONArray("data"))) {
                    List<JSONObject> array1 = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);
                    for (JSONObject children : array1) {
                        InterServerRes childrenRes = new InterServerRes();
                        childrenRes.setId(children.getString("id"));
                        childrenRes.setName(children.getString("name"));
                        childrenList.add(childrenRes);
                    }
                }
                res.setInterServerRes(childrenList);
            }
            resultList.add(res);
        }
        return resultList;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        List<InterAttrRes> resultList = new ArrayList<>();

//        String key = SERVICE_NAME + "_" + "propety" + "_" + gameId + "_" + type;
//        try {
//            List<InterAttrRes> result = (List<InterAttrRes>) redisCache.getCacheObject(key);
//            if (CollectionUtils.isNotEmpty(result)) {
//                return result;
//            }
//        } catch (Exception e) {
//            log.error(SERVICE_NAME + "    redis 读取失败");
//        }
        if (StringUtils.equals(type, "2")) {

            String shuxing = shuxing(gameId, "0", salePartner);
            JSONObject response = JSONObject.parseObject(shuxing);
            List<JSONObject> array = JSONObject.parseArray(response.getString("data"), JSONObject.class);
            for (JSONObject json : array) {
                InterAttrRes res = new InterAttrRes();
                res.setUuid(UuidUtils.generateUuid());
                res.setId(json.getString("id"));
                res.setName(json.getString("rolename"));
                res.setRequired(false);
                {
                    List<InterAttrRes> childrenList = new ArrayList<>();
                    String childrenShuXing = shuxing(gameId, json.getString("id"), salePartner);
                    JSONObject jsonObject = JSONObject.parseObject(childrenShuXing);
                    if (CollectionUtils.isNotEmpty(jsonObject.getJSONArray("data"))) {
                        List<JSONObject> array1 = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);
                        for (JSONObject children : array1) {
                            InterAttrRes childrenRes = new InterAttrRes();
                            childrenRes.setUuid(UuidUtils.generateUuid());
                            childrenRes.setId(children.getString("id"));
                            childrenRes.setName(children.getString("rolename"));
                            childrenRes.setRequired(false);
                            childrenList.add(childrenRes);
                        }
                    }
                    res.setInterServerRes(childrenList);
                }
                resultList.add(res);
            }
        }
//        try {
//            redisCache.setCacheObject(key, resultList, 3, TimeUnit.MINUTES);
//        } catch (Exception e) {
//            log.error(SERVICE_NAME + "    redis 写入失败");
//
//        }
        return resultList;
    }


    static String getAllGame() {
        String url = "https://www.huanhaoba.com/gameid/all-game.json";

        Map<String, Object> params = new TreeMap<>();
        String response = HttpUtils.sendGet(url, JSONObject.toJSONString(params));
        System.out.println(JSONObject.parseObject(response));
        return response;
    }


    public static String getCover1(Map<String, Object> params) {
        StringBuffer resut = new StringBuffer();
        for (String key : params.keySet()) {
            resut.append(key + "=" + params.get(key) + "&");
        }
        return resut.toString();
    }

    static String qufu(String id, SalePartner salePartner) {

        HashMap<String, Object> hashMap = new HashMap<>();
        String key = salePartner.getSaleKey();
//        String key = "asdcza59d2a4e918acbf707bca7905as";
        String appid = salePartner.getOther();
//        String appid = "381";
        Map<String, Object> hhbDto = new HashMap<>();
        hhbDto.put("parentId", id);
        hhbDto.put("appid", appid);
        String json = JSONObject.toJSONString(hhbDto);
        hashMap.put("data", json);
        hashMap.put("sign", DigestUtils.md5Hex(json + key));

        String response = HttpUtils.doPostForm(OutUrlConstant.HHBURL + "/api/open/get-area", hashMap, null);
        System.out.println(response);
        return response;
    }

    static String shuxing(String gameId, String id, SalePartner salePartner) {

        HashMap<String, Object> hashMap = new HashMap<>();
        String key = salePartner.getSaleKey();
//        String key = "asdcza59d2a4e918acbf707bca7905as";
        String appid = salePartner.getOther();
//        String appid = "381";
        Map<String, Object> hhbDto = new HashMap<>();
        hhbDto.put("gameId", gameId);
        hhbDto.put("parentId", id);
        hhbDto.put("appid", appid);
        String json = JSONObject.toJSONString(hhbDto);
        hashMap.put("data", json);
        hashMap.put("sign", DigestUtils.md5Hex(json + key));
        String response = HttpUtils.doPostForm(OutUrlConstant.HHBURL + "/api/open/get-game", hashMap, null);
        return response;
    }


    public static void main(String[] args) {
        qufu("34",null);
    }
}
